#!/bin/bash

# this project absolute path
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)

# binaries build prefix install path
PRFX_PATH=$PRJ_PATH/install

# binaries finally install path if needed
#INST_PATH=/tftp

# config file path
CONF_FILE=$PRJ_PATH/config.json

# shell script will exit once get command error
set -e

#+-------------------------+
#| Shell script functions  |
#+-------------------------+

function pr_error() {
    echo -e "\033[40;31m $1 \033[0m"
}

function pr_warn() {
    echo -e "\033[40;33m $1 \033[0m"
}

function pr_info() {
    echo -e "\033[40;32m $1 \033[0m"
}

# decompress a packet to destination path
function do_unpack()
{
    tarball=$1
    dstpath=`pwd`

    if [[ $# == 2 ]] ; then
        dstpath=$2
    fi

    pr_info "decompress $tarball => $dstpath"

    mkdir -p $dstpath
    case $tarball in
        *.tar.gz)
            tar -xzf $tarball -C $dstpath
            ;;

        *.tar.bz2)
            tar -xjf $tarball -C $dstpath
            ;;

        *.tar.xz)
            tar -xJf $tarball -C $dstpath
            ;;

        *.tar.zst)
            tar -I zstd -xf $tarball -C $dstpath
            ;;

        *.tar)
            tar -xf $tarball -C $dstpath
            ;;

        *.zip)
            unzip -qo $tarball -d $dstpath
            ;;

        *)
            pr_error "decompress Unsupport packet: $tarball"
            return 1;
            ;;
    esac
}

# parser configure file and export environment variable
function do_export()
{
    export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
    export VARIANT=`jq -r ".bsp.variant" $CONF_FILE | tr 'A-Z' 'a-z'`
    export SDKVER=`jq -r ".bsp.sdkver" $CONF_FILE | tr 'A-Z' 'a-z'`
    export TARBALL=`jq -r ".bsp.tarball" $CONF_FILE`
    export BDPATH=`jq -r ".bsp.build" $CONF_FILE`
    export WKSP=$BDPATH/$BOARD/$SDKVER
}

function do_fetch()
{
    cd $PRJ_PATH && mkdir -p $WKSP

    if [ -f $WKSP/u-boot/Makefile ] ; then
        pr_info "SDK $SDKVER source code fetched already"
        if [ ! -e $PRJ_PATH/$SDKVER ] ; then
            ln -sf $WKSP $PRJ_PATH/$SDKVER
        fi
        return 0;
    fi

    pr_info "start fetch $SDKVER source code"

    if [ ! -d $WKSP/.repo ] ; then
        # decompress source code packet
        do_unpack $TARBALL $WKSP
    fi

    if [ ! -s $SDKVER ] ; then
        ln -sf $WKSP $SDKVER
    fi

    cd $PRJ_PATH/$SDKVER && pwd

    if [ ! -e $WKSP/u-boot/Makefile ] ; then
        pr_info "repo sync source code ...."
        .repo/repo/repo sync -l
    fi
}

function do_patch()
{
    PATCH_PATH=$PRJ_PATH/patch
    PATCH_SRCS="buildroot kernel"

    cd $PRJ_PATH/$SDKVER && pwd

    for source in $PATCH_SRCS
    do
        if [ ! -f $source/patched.tag -a -s $PATCH_PATH/$source.patch ] ; then
            pr_warn "do patch for $source now..."
            cd $source && patch -p1 < $PATCH_PATH/$source.patch && cd -
        fi
    done
}

function do_config()
{
    pr_warn "start menuconfig $SDKVER"

    cd $PRJ_PATH/$SDKVER && pwd

    # kernel -> kernel/arch/arm64/configs/rockchip_linux_defconfig
    ./build.sh kconfig

    # buildroot -> buildroot/configs/rockchip_rk3568_defconfig
    ./build.sh bconfig
}

function do_build()
{
    cd $PRJ_PATH/$SDKVER && pwd

    pr_warn "start build $SDKVER"

    pr_info "initial defconfig"
    ./build.sh rk3566_rk3568:rockchip_rk3568_evb1_ddr4_v10_defconfig

    pr_info "start build $VARIANT sdk"
    RK_ROOTFS_SYSTEM=$VARIANT ./build.sh

    echo ""
    pr_warn "output images path: $PRJ_PATH/$SDKVER/rockdev"
    ls $PRJ_PATH/$SDKVER/rockdev
    echo ""
}

function do_clean()
{
    cd $PRJ_PATH/$SDKVER && pwd

    ./build.sh cleanall
}

#+-------------------------+
#| Shell script body entry |
#+-------------------------+

do_export

if [[ $# == 1 && $1 == -c ]] ;then
    pr_warn "start clean $SDKVER"
    do_clean
    exit;
fi

pr_warn "start build $SDKVER for ${BOARD}"

do_fetch

do_patch

do_build

